# coding=utf-8

from html.parser import HTMLParser

# 0

# 1
# <div id="toc">

# 1,            2
# <div id="toc"><div id="toctitle">

# 1,            3
# <div id="toc"><ul class="sectlevel1">

# 1,            3,                     4,  5
# <div id="toc"><ul class="sectlevel1"><li><a href="#introduction">1. Introduction</a>

# 1,            3,                     4,  6,                     7,  8,
# <div id="toc"><ul class="sectlevel1"><li><ul class="sectlevel2"><li><a href="#introduction-whatis">1.1. What is the Vulkan Graphics System?</a></li>

"""
{
        "toctitle":"toc_title",
        "chapters" : [{"chapter_name": "chapter_name1",
                        "chapter_link" : "chapter_link1",
                        "sections" : [{"name":"sec_name",
                                        "link":"link_name"},
                                        ...
                                        ]
                        },
                        ...
                        ]
        }
"""


class MyHTMLParser(HTMLParser):
    def __init__(self):
        super(MyHTMLParser, self).__init__()
        self.states = [0]
        self.in_toc = False

    def state(self):
        #if not self.stat
        assert self.states
        return self.states[-1]

    def push_state(self, n):
        self.states.append(n)

    def pop_state(self):
        self.states.pop()

    def handle_starttag(self, tag, attrs):

        if self.state() == 0:
            if tag == "div":
                assert self.state() in [0, 1]
                if ("id", "toc") in attrs:
                    # print(self.state())
                    assert self.state() == 0
                    self.push_state(1)
        else:
            print(self.states, tag, attrs)
            print("Encountered a start tag:", tag)
            if ("id", "toctitle") in attrs:
                assert self.state() == 1
                self.push_state(2)
            elif tag == "ul":
                assert self.state() in [1, 4]
                if self.state() == 1:
                    assert ("class", "sectlevel1") in attrs
                    self.push_state(3)
                elif self.state() == 4:
                    assert ("class", "sectlevel2") in attrs
                    self.push_state(6)
            elif tag == "li":
                assert self.state() in [3, 6]
                if self.state() == 3:
                    self.push_state(4)
                elif self.state() == 6:
                    self.push_state(7)
            elif tag == "a":
                assert self.state() in [4, 7]
                if self.state() == 4:
                    self.push_state(5)
                elif self.state() == 7:
                    self.push_state(8)
            else:
                pass
            # assert False


    def handle_endtag(self, tag):
        if self.state() > 0:
            print(self.states)
            print("Encountered an end tag :", tag)
            if tag == "li":
                assert self.state() in [4, 7]
                self.pop_state()
            elif tag == "a":
                assert self.state() in [5, 8]
                self.pop_state()
            elif tag == "ul":
                assert self.state() in [3, 6]
                self.pop_state()
            elif tag == "div":
                assert self.state() in [1, 2]
                self.pop_state()
            else:
                # print
                assert False

    def handle_data(self, data):
        if self.state() > 0:
            print("Encountered some data  :", data)

parser = MyHTMLParser()
with open("vkspec.html", encoding="utf-8") as f:
    html = f.read()

def Clean(html):
    html = html.replace("<sub>","")
    html = html.replace("</sub>","")
    return html
html = Clean(html)
parser.feed(html)
